package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.SalesDivision;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.support.BeanKit;
import com.hl.card.modular.packBack.service.GoodsSevice;
import com.hl.card.modular.packBack.service.ISalesDivisionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangpu
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/salesDivision")
public class SalesDivisionController extends BaseController {


    @Resource
    private ISalesDivisionService iSalesDivisionService;

    @Resource
    private GoodsSevice goodsSevice;

    private String PREFIX = "/packback/salesDivision/";

    @RequestMapping("")
    public String index(){
        return PREFIX+"salesDivision.html";
    }
    /**
     * 跳转到添加销售利润管理
     */
    @RequestMapping(value = "/salesDivision_add")
    public Object salesDivisionAdd(Model model){
        List<Map<String, Object>> list= goodsSevice.selectList();

        model.addAttribute("goods",list);
        LogObjectHolder.me().set(list);
        return PREFIX+"salesDivision_add.html";
    }

    /**
     * 跳转到修改销售利润管理
     */
    @RequestMapping(value = "/salesDivision_update/{disProfiParamId}")
    public Object salesDivisionUpdate(@PathVariable Integer disProfiParamId, Model model){
        List<Map<String, Object>> list= goodsSevice.selectList();
        SalesDivision salesDivision = iSalesDivisionService.selectById(disProfiParamId);
        Map<String, Object> salesDivisionmap = BeanKit.beanToMap(salesDivision);
        model.addAttribute("salesDivision",salesDivisionmap);
        model.addAttribute("goods",list);
        LogObjectHolder.me().set(list);
        LogObjectHolder.me().set(salesDivision);

        return PREFIX+"salesDivision_edit.html";
    }

    /**
     * 获取销售利润列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        List<SalesDivision> list= iSalesDivisionService.selectList();
        System.out.println(list);
        return list;
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Integer id){
        iSalesDivisionService.delete(id);
        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SalesDivision salesDivision){
        SalesDivision salesDivision1 = iSalesDivisionService.selectByName(salesDivision.getGoodsCode());
        if(salesDivision1 == null) {
            iSalesDivisionService.save(salesDivision);
            return super.SUCCESS_TIP;
        }else {

            return super.FailuerTip("改产品类型已存在");
        }
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid SalesDivision salesDivision){
        if(salesDivision.getCalculationType() == 0){
            salesDivision.setFixedAmount(0);
        }else if(salesDivision.getCalculationType() == 1){
            salesDivision.setProfitRatio(0);
        }
        iSalesDivisionService.updateById(salesDivision);
        return super.SUCCESS_TIP;
    }



}
